#include <iostream>
#include <vector>
#include <algorithm>

std::string sortString(std::string str) {
    std::vector<int> count(26, 0);
    for (char ch : str) {
        count[ch - 'a']++;
    }
    
    std::string result;
    while (result.length() < str.length()) {
        for (int i = 0; i < 26; ++i) {
            if (count[i] > 0) {
                result += 'a' + i;
                count[i]--;
            }
        }
        
        for (int i = 25; i >= 0; --i) {
            if (count[i] > 0) {
                result += 'a' + i;
                count[i]--;
            }
        }
    }
    
    return result;
}

int main() {
    std::string str = "leetcode";
    std::cout << "Original string: " << str << std::endl;
    
    std::string sortedStr = sortString(str);
    std::cout << "Sorted string: " << sortedStr << std::endl;
    
    return 0;
}